package per.mnxc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import per.mnxc.dto.TotanyDto;
import per.mnxc.mapper.FilesMapper;
import per.mnxc.mapper.TotancyFileMapper;
import per.mnxc.pojo.Files;
import per.mnxc.pojo.TotancyFile;
import per.mnxc.pojo.Totany;
import per.mnxc.mapper.TotanyMapper;
import per.mnxc.service.ITotanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mnxc
 * @since 2022-11-26
 */
@Service
public class TotanyServiceImpl extends ServiceImpl<TotanyMapper, Totany> implements ITotanyService {
    @Autowired
    TotancyFileMapper totancyFileMapper;
    @Autowired
    FilesMapper filesMapper;
    @Autowired
    TotanyMapper totanyMapper;

    @Override
    public IPage<TotanyDto> pagePlus(int page, int pageSize, String name) {
        //1.构造分页器
        IPage<Totany> pageInfo = new Page<>(page,pageSize);
        //2.条件查询
        LambdaQueryWrapper<Totany> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Totany::getTotanyName,name)
                .or().like(name!=null,Totany::getAnotherName,name);
        //查询基本信息
        this.page(pageInfo,lqw);
        //3.查询对应植物的图片url
        IPage<TotanyDto> pageInfoDto = new Page<>();
        BeanUtils.copyProperties(pageInfo,pageInfoDto,"records");
        List<Totany> records = pageInfo.getRecords();
        List<TotanyDto> dtoList = records.stream().map((item)->{//3.1
            TotanyDto totanyDto = new TotanyDto();
            BeanUtils.copyProperties(item,totanyDto);
            Long plantId = item.getTotanyId();
            //获取图片对应的文件id
            LambdaQueryWrapper<TotancyFile> ftLqw= new LambdaQueryWrapper<>();
            ftLqw.eq(TotancyFile::getTotId,plantId);
            TotancyFile totancyFile = totancyFileMapper.selectOne(ftLqw);
            if(totancyFile!=null){
                Files picture = filesMapper.selectById(totancyFile.getFileId());
                //设置图片属性
                totanyDto.setPictureUrl(picture.getPath());
            }
            return totanyDto;
        }).collect(Collectors.toList());
        pageInfoDto.setRecords(dtoList);
        return pageInfoDto;
    }

    @Override
    public String savePlus(TotanyDto totanyDto) {
        //1.保存图片
        Files file = new Files();
        file.setPath(totanyDto.getFileName());
        filesMapper.insert(file);
        Long fileId  = file.getFileId();
        //2.保存植物
        totanyMapper.insert(totanyDto);
        Long plantId = totanyDto.getTotanyId();
        //3.关联
        TotancyFile totancyFile = new TotancyFile();
        totancyFile.setFileId(fileId);
        totancyFile.setTotId(plantId);
        totancyFileMapper.insert(totancyFile);
        //4.返回成功
        return "添加植物信息成功";
    }
}
